#Import Libraries
Import Required Libraries
library(ggplot2)
library(Metrics)
library(plotly) # for interactive plots
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(quantmod)
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
theme_set(theme_minimal())
companies <- c("GOOGLE", "APPLE", "TESLA")
#Load Dataset
Loading datasets for Apple, Google and Tesla
# Load the dataset
stock.df <- read.csv(file="data/all_company_stock.csv")
stock.df <- stock.df[, c("Open","High","Low","Close","Volume","Adjusted","Date", "Company.Name")]
stock.df$Date <- as.Date(stock.df$Date, format="%Y-%m-%d")
head(stock.df)
## Open High Low Close Volume Adjusted Date
## 1 0.936384 1.004464 0.907924 0.999442 535796800 0.856887 2000-01-03
## 2 0.966518 0.987723 0.903460 0.915179 512377600 0.784643 2000-01-04
## 3 0.926339 0.987165 0.919643 0.928571 778321600 0.796124 2000-01-05
## 4 0.947545 0.955357 0.848214 0.848214 767972800 0.727229 2000-01-06
## 5 0.861607 0.901786 0.852679 0.888393 460734400 0.761677 2000-01-07
## 6 0.910714 0.912946 0.845982 0.872768 505064000 0.748281 2000-01-10
## Company.Name
## 1 APPLE
## 2 APPLE
## 3 APPLE
## 4 APPLE
## 5 APPLE
## 6 APPLE
tail(stock.df)
## Open High Low Close Volume Adjusted Date Company.Name
## 12747 1099.47 1108.78 1081.00 1081.92 11680900 1081.92 2021-11-26 TESLA
## 12748 1100.99 1142.67 1100.19 1136.99 19464500 1136.99 2021-11-29 TESLA
## 12749 1144.37 1168.00 1118.00 1144.76 27092000 1144.76 2021-11-30 TESLA
## 12750 1160.70 1172.84 1090.76 1095.00 22816800 1095.00 2021-12-01 TESLA
## 12751 1099.06 1113.00 1056.65 1084.60 24371600 1084.60 2021-12-02 TESLA
## 12752 1084.79 1090.58 1000.21 1014.97 30696300 1014.97 2021-12-03 TESLA
# getting dimensions
for (company in companies) {
print(company)
print(dim(stock.df[stock.df$Company.Name == company, ]))
}
## [1] "GOOGLE"
## [1] 4355 8
## [1] "APPLE"
## [1] 5517 8
## [1] "TESLA"
## [1] 2880 8
# Inspect variables
for (company in companies) {
print(company)
print(str(stock.df[stock.df$Company.Name == company, ]))
}
## [1] "GOOGLE"
## 'data.frame': 4355 obs. of 8 variables:
## $ Open : num 50.1 50.6 55.4 55.7 52.5 ...
## $ High : num 52.1 54.6 56.8 55.9 54.1 ...
## $ Low : num 48 50.3 54.6 51.8 52 ...
## $ Close : num 50.2 54.2 54.8 52.5 53.1 ...
## $ Volume : num 44659096 22834343 18256126 15247337 9188602 ...
## $ Adjusted : num 50.2 54.2 54.8 52.5 53.1 ...
## $ Date : Date, format: "2004-08-19" "2004-08-20" ...
## $ Company.Name: chr "GOOGLE" "GOOGLE" "GOOGLE" "GOOGLE" ...
## NULL
## [1] "APPLE"
## 'data.frame': 5517 obs. of 8 variables:
## $ Open : num 0.936 0.967 0.926 0.948 0.862 ...
## $ High : num 1.004 0.988 0.987 0.955 0.902 ...
## $ Low : num 0.908 0.903 0.92 0.848 0.853 ...
## $ Close : num 0.999 0.915 0.929 0.848 0.888 ...
## $ Volume : num 5.36e+08 5.12e+08 7.78e+08 7.68e+08 4.61e+08 ...
## $ Adjusted : num 0.857 0.785 0.796 0.727 0.762 ...
## $ Date : Date, format: "2000-01-03" "2000-01-04" ...
## $ Company.Name: chr "APPLE" "APPLE" "APPLE" "APPLE" ...
## NULL
## [1] "TESLA"
## 'data.frame': 2880 obs. of 8 variables:
## $ Open : num 3.8 5.16 5 4.6 4 ...
## $ High : num 5 6.08 5.18 4.62 4 ...
## $ Low : num 3.51 4.66 4.05 3.74 3.17 ...
## $ Close : num 4.78 4.77 4.39 3.84 3.22 ...
## $ Volume : num 93831500 85935500 41094000 25699000 34334500 ...
## $ Adjusted : num 4.78 4.77 4.39 3.84 3.22 ...
## $ Date : Date, format: "2010-06-29" "2010-06-30" ...
## $ Company.Name: chr "TESLA" "TESLA" "TESLA" "TESLA" ...
## NULL
#Summary of Data
#summary of the stocks for each company
for (company in companies) {
print(company)
print(summary(stock.df[stock.df$Company.Name == company, ]))
}
## [1] "GOOGLE"
## Open High Low Close
## Min. : 49.64 Min. : 50.92 Min. : 48.03 Min. : 50.06
## 1st Qu.: 246.68 1st Qu.: 249.32 1st Qu.: 243.31 1st Qu.: 246.22
## Median : 408.53 Median : 410.56 Median : 405.33 Median : 407.76
## Mean : 656.98 Mean : 663.17 Mean : 650.53 Mean : 657.06
## 3rd Qu.: 960.43 3rd Qu.: 966.04 3rd Qu.: 953.10 3rd Qu.: 961.41
## Max. :2999.51 Max. :3019.33 Max. :2977.98 Max. :2996.77
## Volume Adjusted Date Company.Name
## Min. : 465600 Min. : 50.06 Min. :2004-08-19 Length:4355
## 1st Qu.: 1713150 1st Qu.: 246.22 1st Qu.:2008-12-13 Class :character
## Median : 3865531 Median : 407.76 Median :2013-04-15 Mode :character
## Mean : 6525447 Mean : 657.06 Mean :2013-04-12
## 3rd Qu.: 8147644 3rd Qu.: 961.41 3rd Qu.:2017-08-08
## Max. :82151167 Max. :2996.77 Max. :2021-12-03
## [1] "APPLE"
## Open High Low Close
## Min. : 0.232 Min. : 0.2355 Min. : 0.2271 Min. : 0.2343
## 1st Qu.: 1.532 1st Qu.: 1.5500 1st Qu.: 1.5125 1st Qu.: 1.5286
## Median : 11.490 Median : 11.5529 Median : 11.3936 Median : 11.4732
## Mean : 23.764 Mean : 24.0154 Mean : 23.5135 Mean : 23.7747
## 3rd Qu.: 29.545 3rd Qu.: 29.8250 3rd Qu.: 29.3350 3rd Qu.: 29.6575
## Max. :167.480 Max. :170.3000 Max. :164.5300 Max. :165.3000
## Volume Adjusted Date Company.Name
## Min. :3.934e+07 Min. : 0.2009 Min. :2000-01-03 Length:5517
## 1st Qu.:1.608e+08 1st Qu.: 1.3105 1st Qu.:2005-06-29 Class :character
## Median :3.168e+08 Median : 9.8367 Median :2010-12-20 Mode :character
## Mean :4.303e+08 Mean : 22.6863 Mean :2010-12-20
## 3rd Qu.:5.717e+08 3rd Qu.: 27.4195 3rd Qu.:2016-06-14
## Max. :7.422e+09 Max. :165.3000 Max. :2021-12-03
## [1] "TESLA"
## Open High Low Close
## Min. : 3.228 Min. : 3.326 Min. : 2.996 Min. : 3.16
## 1st Qu.: 13.547 1st Qu.: 14.484 1st Qu.: 12.339 1st Qu.: 13.38
## Median : 46.050 Median : 46.896 Median : 45.315 Median : 46.05
## Mean : 117.412 Mean : 119.904 Mean : 114.798 Mean : 117.50
## 3rd Qu.: 66.082 3rd Qu.: 67.426 3rd Qu.: 65.121 3rd Qu.: 66.31
## Max. :1234.410 Max. :1243.490 Max. :1217.000 Max. :1229.91
## Volume Adjusted Date Company.Name
## Min. : 592500 Min. : 3.16 Min. :2010-06-29 Length:2880
## 1st Qu.: 12726375 1st Qu.: 13.38 1st Qu.:2013-05-08 Class :character
## Median : 24956500 Median : 46.05 Median :2016-03-17 Mode :character
## Mean : 31447782 Mean : 117.50 Mean :2016-03-17
## 3rd Qu.: 40088875 3rd Qu.: 66.31 3rd Qu.:2019-01-28
## Max. :304694000 Max. :1229.91 Max. :2021-12-03
#Plots and Charts
Plotting close value over the time for each stock data set;
## Graph Returns closing prices
plt <- ggplot(stock.df, aes(x= Date, y = Close, colour = Company.Name)) +
geom_line() + theme_bw() + labs(title = "", x = "Date", y= "Closing Price", subtitle = "")
ggplotly(plt)
## Graph Returns Volume traded
plt <- ggplot(stock.df, aes(x= Date, y = Volume, colour = Company.Name)) +
geom_line() + theme_bw() + labs(title = "", x = "Date", y= "Volume", subtitle = "")
ggplotly(plt)
# Area plot
ggplot(stock.df) +
geom_area(aes(x = Date, y = Close, color = Company.Name, fill = Company.Name),
alpha = 0.5, position = position_dodge(0.8)) +
scale_color_manual(values = c("red", "gray" , "blue")) +
scale_fill_manual(values = c("red", "gray" , "blue"))
df<- stock.df[stock.df$Company.Name == "GOOGLE", ]
# Multiple line plot
p <- ggplot(df) +
geom_line(aes(x = Date, y = Low, color = "Low")) +
geom_line(aes(x = Date, y = High, color = "High")) +
labs(title = "", x = "Date", y= "High and Low Values", subtitle = "Google Dataset", color = "Google") +
theme_classic()
p
df<- stock.df[stock.df$Company.Name == "APPLE", ]
# Multiple line plot
p <- ggplot(df) +
geom_line(aes(x = Date, y = Low, color = "Low")) +
geom_line(aes(x = Date, y = High, color = "High")) +
labs(title = "", x = "Date", y= "High and Low Values", subtitle = "Apple Dataset", color = "Apple") +
theme_classic()
p
df<- stock.df[stock.df$Company.Name == "TESLA", ]
# Multiple line plot
p <- ggplot(df) +
geom_line(aes(x = Date, y = Low, color = "Low")) +
geom_line(aes(x = Date, y = High, color = "High")) +
labs(title = "", x = "Date", y= "High and Low Values", subtitle = "Tesla Dataset", color = "Tesla") +
theme_classic()
p
data <- stock.df[stock.df$Company.Name == "GOOGLE", ]
rownames(data)<- data$Date
data <- data[, c('Close', 'Open', 'High', 'Low', 'Volume', 'Adjusted')]
head(data)
## Close Open High Low Volume Adjusted
## 2004-08-19 50.22022 50.05005 52.08208 48.02803 44659096 50.22022
## 2004-08-20 54.20921 50.55556 54.59460 50.30030 22834343 54.20921
## 2004-08-23 54.75475 55.43043 56.79680 54.57958 18256126 54.75475
## 2004-08-24 52.48749 55.67567 55.85586 51.83684 15247337 52.48749
## 2004-08-25 53.05306 52.53253 54.05405 51.99199 9188602 53.05306
## 2004-08-26 54.00901 52.52753 54.02903 52.38238 7094898 54.00901
chartSeries(data)
# Add Moving average convergence divergence
addMACD()
# Add Bollinger Bands to the stock price
addBBands()
# Add Commodity Channel Index
addCCI()
# Add Directional Movement Indicator
addADX()
# Add Money flow Chart
addCMF()
# Highest and Lowest Point of Price
seriesHi(data[, c("Close")])
## [1] 2996.77
seriesLo(data[, c("Close")])
## [1] 50.05505
#Return Analysis
# Daily Returns
daily.return <- dailyReturn(data)
tail(daily.return)
## daily.returns
## 2021-11-26 0
## 2021-11-29 0
## 2021-11-30 0
## 2021-12-01 0
## 2021-12-02 0
## 2021-12-03 0
# Weakly Returns
weekly.return <- weeklyReturn(data)
tail(weekly.return)
## weekly.returns
## 2021-10-29 0.06316144
## 2021-11-05 0.02452478
## 2021-11-12 -0.01879929
## 2021-11-19 0.02398718
## 2021-11-26 -0.04739115
## 2021-12-03 -0.01086771
# Monthly Returns
monthly.return <- monthlyReturn(data)
tail(monthly.return)
## monthly.returns
## 2021-07-30 0.106304258
## 2021-08-31 0.074678787
## 2021-09-30 -0.074334109
## 2021-10-29 0.083062484
## 2021-11-30 -0.021071611
## 2021-12-03 -0.009184228
# Yearly Returns
yearly.return <- yearlyReturn(data)
tail(yearly.return)
## yearly.returns
## 2016-12-30 0.01582358
## 2017-12-29 0.33317287
## 2018-12-31 -0.01867570
## 2019-12-31 0.28952408
## 2020-12-31 0.30324365
## 2021-12-03 0.61628722
#all returns
all.returns <- allReturns(data)
head(all.returns)
## daily weekly monthly quarterly yearly
## 2004-08-19 0 NA NA NA NA
## 2004-08-20 0 0.08310002 NA NA NA
## 2004-08-23 0 NA NA NA NA
## 2004-08-24 0 NA NA NA NA
## 2004-08-25 0 NA NA NA NA
## 2004-08-26 0 NA NA NA NA
data <- stock.df[stock.df$Company.Name == "APPLE", ]
rownames(data)<- data$Date
data <- data[, c('Close', 'Open', 'High', 'Low', 'Volume', 'Adjusted')]
head(data)
## Close Open High Low Volume Adjusted
## 2000-01-03 0.999442 0.936384 1.004464 0.907924 535796800 0.856887
## 2000-01-04 0.915179 0.966518 0.987723 0.903460 512377600 0.784643
## 2000-01-05 0.928571 0.926339 0.987165 0.919643 778321600 0.796124
## 2000-01-06 0.848214 0.947545 0.955357 0.848214 767972800 0.727229
## 2000-01-07 0.888393 0.861607 0.901786 0.852679 460734400 0.761677
## 2000-01-10 0.872768 0.910714 0.912946 0.845982 505064000 0.748281
chartSeries(data)
# Add Moving average convergence divergence
addMACD()
# Add Bollinger Bands to the stock price
addBBands()
# Add Commodity Channel Index
addCCI()
# Add Directional Movement Indicator
addADX()
# Add Money flow Chart
addCMF()
# Highest and Lowest Point of Price
seriesHi(data[, c("Close")])
## [1] 165.3
seriesLo(data[, c("Close")])
## [1] 0.234286
#Return Analysis
# Daily Returns
daily.return <- dailyReturn(data)
tail(daily.return)
## daily.returns
## 2021-11-26 0
## 2021-11-29 0
## 2021-11-30 0
## 2021-12-01 0
## 2021-12-02 0
## 2021-12-03 0
# Weakly Returns
weekly.return <- weeklyReturn(data)
tail(weekly.return)
## weekly.returns
## 2021-10-29 -0.015002658
## 2021-11-05 0.024929949
## 2021-11-12 -0.017193136
## 2021-11-19 0.061364275
## 2021-11-26 -0.001086041
## 2021-12-03 0.021488872
# Monthly Returns
monthly.return <- monthlyReturn(data)
tail(monthly.return)
## monthly.returns
## 2021-07-30 0.060646252
## 2021-08-31 0.049822996
## 2021-09-30 -0.066164284
## 2021-10-29 0.036310908
## 2021-11-30 0.092275074
## 2021-12-03 -0.001250607
# Yearly Returns
yearly.return <- yearlyReturn(data)
tail(yearly.return)
## yearly.returns
## 2016-12-30 0.10122114
## 2017-12-29 0.46599671
## 2018-12-31 -0.07528663
## 2019-12-31 0.85020447
## 2020-12-31 0.81983982
## 2021-12-03 0.21257212
#all returns
all.returns <- allReturns(data)
head(all.returns)
## daily weekly monthly quarterly yearly
## 2000-01-03 0 NA NA NA NA
## 2000-01-04 0 NA NA NA NA
## 2000-01-05 0 NA NA NA NA
## 2000-01-06 0 NA NA NA NA
## 2000-01-07 0 -0.05125141 NA NA NA
## 2000-01-10 0 NA NA NA NA
#Tesla Stock Analysis
data <- stock.df[stock.df$Company.Name == "TESLA", ]
rownames(data)<- data$Date
data <- data[, c('Close', 'Open', 'High', 'Low', 'Volume', 'Adjusted')]
head(data)
## Close Open High Low Volume Adjusted
## 2010-06-29 4.778 3.800 5.000 3.508 93831500 4.778
## 2010-06-30 4.766 5.158 6.084 4.660 85935500 4.766
## 2010-07-01 4.392 5.000 5.184 4.054 41094000 4.392
## 2010-07-02 3.840 4.600 4.620 3.742 25699000 3.840
## 2010-07-06 3.222 4.000 4.000 3.166 34334500 3.222
## 2010-07-07 3.160 3.280 3.326 2.996 34608500 3.160
chartSeries(data)
# Add Moving average convergence divergence
addMACD()
# Add Bollinger Bands to the stock price
addBBands()
# Add Commodity Channel Index
addCCI()
# Add Directional Movement Indicator
addADX()
# Add Money flow Chart
addCMF()
# Highest and Lowest Point of Price
seriesHi(data[, c("Close")])
## [1] 1229.91
seriesLo(data[, c("Close")])
## [1] 3.16
#Return Analysis
# Daily Returns
daily.return <- dailyReturn(data)
tail(daily.return)
## daily.returns
## 2021-11-26 0
## 2021-11-29 0
## 2021-11-30 0
## 2021-12-01 0
## 2021-12-02 0
## 2021-12-03 0
# Weakly Returns
weekly.return <- weeklyReturn(data)
tail(weekly.return)
## weekly.returns
## 2021-10-29 0.20455456
## 2021-11-05 0.12559522
## 2021-11-12 -0.15629138
## 2021-11-19 0.07211532
## 2021-11-26 -0.01070738
## 2021-12-03 -0.07473633
# Monthly Returns
monthly.return <- monthlyReturn(data)
tail(monthly.return)
## monthly.returns
## 2021-07-30 -0.01347806
## 2021-08-31 0.08585949
## 2021-09-30 0.06684654
## 2021-10-29 0.38478705
## 2021-11-30 0.04173465
## 2021-12-03 -0.10535776
# Yearly Returns
yearly.return <- yearlyReturn(data)
tail(yearly.return)
## yearly.returns
## 2016-12-30 -0.11196881
## 2017-12-29 0.46447471
## 2018-12-31 0.04922584
## 2019-12-31 0.23618025
## 2020-12-31 7.59430953
## 2021-12-03 0.44723062
#all returns
all.returns <- allReturns(data)
head(all.returns)
## daily weekly monthly quarterly yearly
## 2010-06-29 0 NA NA NA NA
## 2010-06-30 0 NA 0.2542105 0.2542105 NA
## 2010-07-01 0 NA NA NA NA
## 2010-07-02 0 0.01052632 NA NA NA
## 2010-07-06 0 NA NA NA NA
## 2010-07-07 0 NA NA NA NA